package com.bob.light.activity.delegate

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.bob.light.TuyaHomeSDKHelper
import com.bob.light.config.DeviceConfig
import com.bob.light.toast
import com.tuya.smart.home.sdk.builder.ActivatorBuilder
import com.tuya.smart.sdk.api.ITuyaActivator
import com.tuya.smart.sdk.bean.DeviceBean

class ActivatorManagerDelegate : LifecycleObserver {

    private var mTuyaActivator: ITuyaActivator? = null

    fun start(builder: ActivatorBuilder) {
        if (mTuyaActivator != null) {
            onDestroy()
        }
        mTuyaActivator = TuyaHomeSDKHelper.initTuyaActivator(builder)
        mTuyaActivator?.start()
    }

    fun onActiveSuccess(devResp: DeviceBean?) {
        DeviceConfig.save(devResp)
    }

    fun onActiveError(errorMsg: String?) {
        errorMsg?.toast()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onStop() {
        mTuyaActivator?.stop()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy() {
        mTuyaActivator?.onDestroy()
        mTuyaActivator = null
    }


}